package com.bangdao.app.jetpacktest.jetpack.databinding.bindingadapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.bangdao.app.jetpacktest.R;
import com.bumptech.glide.Glide;

/**
 * @ProjectName: JetpackTest
 * @ClassName: ImageViewBindingAdapter
 * @Description: java类作用描述
 * @Author: luhui
 * @Email: hui.lu@bangdao-tech.com
 * @CreateDate: 2022/6/4 18:12
 * @UpdateUser: luhui
 * @UpdateDate: 2022/6/4 18:12
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ImageViewBindingAdapter {

    /**
     * 加载网络图
     * @param imageView
     * @param imageUrl
     */
    @BindingAdapter("image")
    public static void setImage(ImageView imageView, String imageUrl){
        if(!TextUtils.isEmpty(imageUrl)){
            Glide.with(imageView.getContext())
                    .load(imageUrl)
                    .placeholder(R.mipmap.ic_launcher)
                    .into(imageView);
        }else{
            imageView.setBackgroundColor(Color.DKGRAY);
        }
    }

    /**
     * 加载本地图
     * @param imageView
     * @param imageResource
     */
    @BindingAdapter("image")
    public static void setImage(ImageView imageView, int imageResource){
        imageView.setImageResource(imageResource);
    }

    /**
     * 多参数重载，优先加载网络图
     * @param imageView
     * @param imageUrl
     * @param imageResource
     */
    @BindingAdapter(value = {"image", "defaultImageResource"}, requireAll = false)
    public static void setImage(ImageView imageView, String imageUrl, int imageResource){
        if(!TextUtils.isEmpty(imageUrl)){
            Glide.with(imageView.getContext())
                    .load(imageUrl)
                    .placeholder(R.mipmap.ic_launcher)
                    .into(imageView);
        }else {
            imageView.setImageResource(imageResource);
        }
    }
}
